package viettravel.guiandevent.map;

import java.util.ArrayList;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;

public class GooglemapCommon {

	public static void addMarker(GoogleMap gMap, MarkerOptions markerOption) {
		gMap.addMarker(markerOption);
	}

	public static void addMarker(GoogleMap gMap, LatLng latLng, String title,
			String snippet, Drawable icon) {
		gMap.addMarker(new MarkerOptions()
				.position(latLng)
				.title(title)
				.snippet(snippet)
				.icon(BitmapDescriptorFactory
						.fromBitmap(((BitmapDrawable) icon).getBitmap())));
	}

	public static void addMarker(Context context, GoogleMap gMap,
			LatLng latLng, int title, int snippet, int icon) {
		gMap.addMarker(new MarkerOptions().position(latLng)
				.title(context.getString(title))
				.icon(BitmapDescriptorFactory.fromResource(icon))
				.snippet(context.getString(snippet)));
	}

	public static void addMarker(GoogleMap gMap, LatLng latLng, int icon) {
		gMap.addMarker(new MarkerOptions().position(latLng).icon(
				BitmapDescriptorFactory.fromResource(icon)));
	}

	public static void drawDirection(GoogleMap gMap, PolylineOptions polyOtion,
			ArrayList<LatLng> listLatLng, int fromIcon, int toIcon) {
		ArrayList<LatLng> directionPoint = listLatLng;

		for (int i = 0; i < directionPoint.size(); i++) {
			polyOtion.add(directionPoint.get(i));
		}
		gMap.addPolyline(polyOtion);
		addMarker(gMap, directionPoint.get(0), fromIcon);
		addMarker(gMap, directionPoint.get(directionPoint.size() - 1), toIcon);
	}

	public static ArrayList<LatLng> decodePoly(String encoded) {
		ArrayList<LatLng> poly = new ArrayList<LatLng>();
		int index = 0, len = encoded.length();
		int lat = 0, lng = 0;

		while (index < len) {
			int b, shift = 0, result = 0;
			do {
				b = encoded.charAt(index++) - 63;
				result |= (b & 0x1f) << shift;
				shift += 5;
			} while (b >= 0x20);
			int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
			lat += dlat;

			shift = 0;
			result = 0;
			do {
				b = encoded.charAt(index++) - 63;
				result |= (b & 0x1f) << shift;
				shift += 5;
			} while (b >= 0x20);
			int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
			lng += dlng;

			LatLng p = new LatLng((((double) lat / 1E5)),
					(((double) lng / 1E5)));
			poly.add(p);
		}
		return poly;
	}

}
